#include<stdio.h>
#include<stdlib.h>
#include"queue.h"

void InitQueue(Queue *queue)
{
     QueueNode *node = malloc(sizeof(QueueNode));
     queue->front = queue->tail = node;
     queue->tail->next = NULL;          //尾结点的下一个结点为空
}
int EnQueue(Queue *queue,datatypeQ i)
{
     QueueNode *current = malloc(sizeof(QueueNode));
     if(current == NULL)
          return -1;
     current->data = i;
     current->next = NULL;
     queue->tail->next = current;
     queue->tail = current;
     return 0;
}
int DeQueue(Queue *queue)
{
     if (queue->front == queue->tail)
          return -1;
     QueueNode * current = queue->front->next;
     queue->front->next = current->next;
     if (current == queue->tail)
          queue->tail = queue->front;
     free(current);
     return 0;
}
int GetHead(Queue *queue,datatypeQ *v)
{
     if(queue->front == queue->tail)
          return -1;
     QueueNode *current = queue->front->next;
     *v = current->data;
     return 0;
}
int Length(Queue *queue)
{
     int len = 0;
     QueueNode *current = queue->front->next;
     while (current != NULL)
     {
          ++len;
          current = current->next;
     }
     return len;
}
void CleanQueue(Queue *queue)
{
     if (queue->front == queue->tail)
          return;
     QueueNode *current;
     while (current = queue->front->next)
     {
          queue->front->next = current->next;
          free(current);
     }
     queue->tail = queue->front;
}
void DestoryQueue(Queue *queue)
{
     CleanQueue(queue);
     free(queue->front);
     queue->front = queue->tail = NULL;    //预防野指针
}

void ShowQueue(Queue *queue)
{
     QueueNode *current = queue->front->next;
     printf("Front:>  ");
     while (current != NULL)
     {
          printf("%d  ", current->data);
          current = current->next;
     }
     printf("<Tail:\n");
}
int EmptyQueue(Queue *queue)
{
     return queue->front == queue->tail;
}
